<?php

namespace App\Http\Admin\Controllers\Goods;

use App\Http\Controllers\Controller;
use App\Http\Models\Good;
use App\Http\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class GoodsController extends Controller
{

    public function index()
    {
        return view('goods.goods_list');
    }

    public function app()
    {
        return view('app.product');
    }

    public function que()
    {
        return view('app.que');
    }

    public function prodView(Request $request){
        $prod_no = $request->input('prod_no') ?? '';
        $item = Product::where('prod_no',$prod_no)->with('skus')->first();
        $colors = [];
        $sizes = [];
        if(!empty($item->skus)){
            foreach ($item->skus as $sku){
                if(!in_array($sku->sku_color,$colors)){
                    $colors[] = $sku->sku_color;
                }
                if(!in_array($sku->sku_size,$sizes)){
                    $sizes[] = $sku->sku_size;
                }
            }
        }
        $item->colors = !empty($colors) ? implode(',', $colors) : '';
        $item->sizes = !empty($sizes) ? implode(',', $sizes) : '';

        return view('goods.prodview',[
            'item' => $item
        ]);
    }

    public function listData(Request $request)
    {

        $prod_no = $request->input('prod_no') ?? '';
        $prod_out_no = $request->input('prod_out_no') ?? '';
        $manu_name = $request->input('manu_name') ?? '';
        $creator = $request->input('creator') ?? '';
        $page = $request->input('page_size') ?? 20;

        $data = Product::with('skus');
        if (!empty($prod_no)) {
            $data = $data->where('prod_no',$prod_no);
        }
        if (!empty($prod_out_no)) {
            $data = $data->where('prod_out_no',$prod_out_no);
        }
        if (!empty($manu_name)) {
            $data = $data->where('manu_name',$manu_name);
        }
        if (!empty($creator)) {
            $data = $data->where('creator',$creator);
        }

        $data = $data->orderBy('created_at','desc')->paginate($page);
        return $data;
    }

}